package com.lk.servlet;

import com.lk.entity.User;
import com.lk.service.UserService;
import com.lk.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userPage")
public class UserSelectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keyword= request.getParameter("keyword");
        UserService userService=new UserServiceImpl();
        if (keyword!=null && !keyword.equals("")){
            int key=Integer.parseInt(keyword);
            List<User> list=userService.selectById(key);
            request.setAttribute("list",list);


        } else if (keyword.equals("")) {
            List<User> list= userService.selectAll();
            request.setAttribute("list",list);
        }
        request.getRequestDispatcher("userList.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
